﻿using System;
using System.Windows.Data;
using WhiteFlow.DevTrackingTool.UI.Models;

namespace WhiteFlow.DevTrackingTool.UI.Converters
{
    public class ExpenseTypeConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Converts the input value to an object of the specified target type.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type to which the value must be converted.</param>
        /// <param name="parameter">The parameter for the conversion.</param>
        /// <param name="culture">The culture to be used in the conversion.</param>
        /// <returns>The input value converted to the target type.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(string) ||
                targetType == typeof(object))
            {
                if (value == null) return "NotSpecified";
                else
                {
                    ExpenseType type = (ExpenseType)Enum.Parse(typeof(ExpenseType), System.Convert.ToString(value));

                    switch (type)
                    {
                        case ExpenseType.NotSpecified:
                            return String.Empty;
                        case ExpenseType.HighwayTickets:
                            return "Highway Tickets";
                        case ExpenseType.Accomodation:
                            return "Accomodation";
                        case ExpenseType.Meal:
                            return "Meal";
                        case ExpenseType.Fuel:
                            return "Fuel";
                        default:
                            return "Not Specified";
                    }
                }
            }
            else throw new Exception("Conversion to the specified type is not currently implemented.");
        }

        /// <summary>
        /// Does the same work of the <c>Convert</c> method, but with reverse logic.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type to which the value must be converted.</param>
        /// <param name="parameter">The parameter for the conversion.</param>
        /// <param name="culture">The culture to be used in the conversion.</param>
        /// <returns>The input value converted to the target type.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return ExpenseType.NotSpecified;

            if (targetType == typeof(ExpenseType))
            {
                string typeString = System.Convert.ToString(value);

                if (String.IsNullOrEmpty(typeString)) return ExpenseType.NotSpecified;

                switch (typeString)
                {
                    case "":
                        return ExpenseType.NotSpecified;
                    case "Highway Tickets":
                        return ExpenseType.HighwayTickets;
                    case "Accomodation":
                        return ExpenseType.Accomodation;
                    case "Meal":
                        return ExpenseType.Meal;
                    case "Fuel":
                        return ExpenseType.Fuel;
                    default:
                        return ExpenseType.NotSpecified;
                }
            }
            else throw new Exception("Conversion to the specified type is not currently implemented.");
        }

        #endregion
    }
}